// pages/tests/test.js
import * as api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrls: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  login() {
    wx.login()
        .then(res => {
          console.log(res)
          if (res.errMsg == 'login:ok') {
            let code = res.code
            api.userLogin(code)
              .then(res => {
                console.log(res)
                if (res.code == 1) {
                  let data = res.data
                  wx.setStorageSync('userinfo', data.userinfo)
                  wx.setStorageSync('token', data.userinfo.token)
                }
              })
              .catch(err => console.log(err));
          }
        })
        .catch(err => console.log(err));
  },
  upload() {
    const that = this
    that.imageUrls = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res)
        if (res.errMsg == 'chooseMedia:ok') {
          let tempFiles = res.tempFiles
          console.log(tempFiles)
          tempFiles.forEach(tempFile => {
            api.upload(tempFile.tempFilePath)
              .then(res => {
                let data = res.data
                let url = data.url
                that.imageUrls.push(url)
              })
              .catch(err => console.log(err))
          });
        }
      }
    })
  },
  printImageUrls() {
    console.log(this.imageUrls)
  },
  checkToken() {
    api.checkToken()
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
  customerCreateProductOrder() {
    let business_user_id = 7,product_id = 1,num = 1
    api.customerCreateProductOrder(business_user_id,product_id,num)
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
  pay() {
    // 测试单号，每次重新生成
    let order_sn = 'ZPO202302171231174211';
    api.pay(order_sn)
      .then(res => {
        console.log(res)
        wx.requestPayment(res.data)
      })
      .catch(err => console.log(err))
  },
  customerInstallOrderList() {
    api.customerInstallOrderList()
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
  customerInstallOrderDetail() {
    const order_sn = '13123'
    api.customerInstallOrderDetail(order_sn)
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
  workerInstallOrderList() {
    api.workerInstallOrderList()
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
  workerInstallOrderDetail() {
    const order_sn = '13123'
    api.workerInstallOrderDetail(order_sn)
      .then(res => console.log(res))
      .catch(err => console.log(err))
  },
})